package com.turing.usdtpay.mvp.view.webwrapper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;

import java.lang.reflect.Field;

/**
 * Created by $Jay
 * Date : 2019\2\12 0012
 **/
public class SDKWebView extends WebView {

    public SDKWebView(Context context) {
        super(context);
        init();
    }

    public SDKWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SDKWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @SuppressLint({"SetJavaScriptEnabled"})
    public void init(){
        setInitialScale(0);
        setVerticalScrollBarEnabled(false);
        setHorizontalScrollBarEnabled(false);
        requestFocusFromTouch();
        requestFocus();
        setFocusable(true);
        WebSettings localWebSettings = getSettings();
        localWebSettings.setDefaultTextEncodingName("utf-8");
        localWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        localWebSettings.setNeedInitialFocus(true);
        localWebSettings.setSaveFormData(false);
        localWebSettings.setSavePassword(false);

        localWebSettings.setBuiltInZoomControls(false);
        localWebSettings.setSupportZoom(true);
        localWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        localWebSettings.setJavaScriptEnabled(true);
        localWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        localWebSettings.setAllowFileAccess(true);

        /**
         * 防止输入框点击后页面放大
         */
        setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    try {
                        Field defaultScale = WebView.class.getDeclaredField("mDefaultScale");
                        defaultScale.setAccessible(true);
                        // WebViewSettingUtil.getInitScaleValue(VideoNavigationActivity.this,
                        // false )/100.0f 是我的程序的??个方法，可以用float 的scale替代
                        defaultScale.setFloat(this, 1);
                    } catch (Exception e) {
                        //
                    }
                }
            }
        });
    }

}
